const Vue = require('vue')
const express = require('express')
const fs = require('fs')

const renderer = require('vue-server-renderer').createRenderer({
  template: fs.readFileSync('./index.template.html', 'utf-8')
})
const server = express()

server.get('/', (req, res) => {
  const app = new Vue({
    template: `
      <div id="app">
        <h1>{{ message }}</h1>
      </div>
    `,
    data: {
      message: 'Hello SSR啊'
    }
  })
  
  renderer.renderToString(app, {
    title: '拉钩教育',
    meta: `
      <meta name="description" content="拉钩教育"></meta>
    `
  }, (err, html) => {
    if(err) {
      return res.status(500).end('Interal Server Error')
    }
    
    res.setHeader('Content-Type', 'text/html; charset=utf8')
    res.end(html)
  })
})

server.listen(3000, () => {
  console.log('server running at: http://localhost:3000')
})
